package com.yunke.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunke.common.log.BusinessLog;
import com.yunke.common.result.YunkeResult;
import com.yunke.dto.circle.YunkeCircleCommentDto;
import com.yunke.dto.circle.YunkeCirclePublishDto;
import com.yunke.service.YunkeCircleService;
import com.yunke.vo.circle.YunkeCircleCommentVo;
import com.yunke.vo.circle.YunkeCircleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

/**
 * 社交圈控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/circle")
@RequiredArgsConstructor
@Validated
@Tag(name = "社交圈", description = "社交圈动态、评论、点赞相关接口")
public class YunkeCircleController {

    private final YunkeCircleService circleService;

    /**
     * 发布动态
     */
    @PostMapping("/publish")
    @Operation(summary = "发布动态", description = "发布社交圈动态")
    @BusinessLog(value = "发布动态", params = "type,content")
    public YunkeResult<Long> publishCircle(@Valid @RequestBody YunkeCirclePublishDto publishDto) {
        Long circleId = circleService.publishCircle(publishDto);
        return YunkeResult.success(circleId);
    }

    /**
     * 获取动态列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取动态列表", description = "分页获取社交圈动态列表")
    @BusinessLog(value = "获取动态列表")
    public YunkeResult<Page<YunkeCircleVo>> getCircleList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "动态类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "位置筛选") @RequestParam(required = false) String location) {
        Page<YunkeCircleVo> result = circleService.getCircleList(current, size, type, location);
        return YunkeResult.success(result);
    }

    /**
     * 获取动态详情
     */
    @GetMapping("/detail/{circleId}")
    @Operation(summary = "获取动态详情", description = "根据ID获取动态详情")
    @BusinessLog(value = "获取动态详情", params = "circleId")
    public YunkeResult<YunkeCircleVo> getCircleDetail(
            @Parameter(description = "动态ID") @PathVariable @NotNull Long circleId) {
        YunkeCircleVo result = circleService.getCircleDetail(circleId);
        return YunkeResult.success(result);
    }

    /**
     * 点赞/取消点赞动态
     */
    @PostMapping("/like/{circleId}")
    @Operation(summary = "点赞/取消点赞动态", description = "对动态进行点赞或取消点赞")
    @BusinessLog(value = "点赞/取消点赞动态", params = "circleId")
    public YunkeResult<Boolean> likeCircle(
            @Parameter(description = "动态ID") @PathVariable @NotNull Long circleId) {
        boolean result = circleService.likeCircle(circleId);
        return YunkeResult.success(result);
    }

    /**
     * 发表评论
     */
    @PostMapping("/comment")
    @Operation(summary = "发表评论", description = "对动态发表评论或回复")
    @BusinessLog(value = "发表评论", params = "circleId,content")
    public YunkeResult<Long> addComment(@Valid @RequestBody YunkeCircleCommentDto commentDto) {
        Long commentId = circleService.addComment(commentDto);
        return YunkeResult.success(commentId);
    }

    /**
     * 获取评论列表
     */
    @GetMapping("/commentList/{circleId}")
    @Operation(summary = "获取评论列表", description = "分页获取动态评论列表")
    @BusinessLog(value = "获取评论列表", params = "circleId")
    public YunkeResult<Page<YunkeCircleCommentVo>> getCommentList(
            @Parameter(description = "动态ID") @PathVariable @NotNull Long circleId,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size) {
        Page<YunkeCircleCommentVo> result = circleService.getCommentList(circleId, current, size);
        return YunkeResult.success(result);
    }

    /**
     * 点赞/取消点赞评论
     */
    @PostMapping("/commentLike/{commentId}")
    @Operation(summary = "点赞/取消点赞评论", description = "对评论进行点赞或取消点赞")
    @BusinessLog(value = "点赞/取消点赞评论", params = "commentId")
    public YunkeResult<Boolean> likeComment(
            @Parameter(description = "评论ID") @PathVariable @NotNull Long commentId) {
        boolean result = circleService.likeComment(commentId);
        return YunkeResult.success(result);
    }

    /**
     * 删除动态
     */
    @DeleteMapping("/delete/{circleId}")
    @Operation(summary = "删除动态", description = "删除自己发布的动态")
    @BusinessLog(value = "删除动态", params = "circleId")
    public YunkeResult<Boolean> deleteCircle(
            @Parameter(description = "动态ID") @PathVariable @NotNull Long circleId) {
        boolean result = circleService.deleteCircle(circleId);
        return YunkeResult.success(result);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/deleteComment/{commentId}")
    @Operation(summary = "删除评论", description = "删除自己的评论或自己动态下的评论")
    @BusinessLog(value = "删除评论", params = "commentId")
    public YunkeResult<Boolean> deleteComment(
            @Parameter(description = "评论ID") @PathVariable @NotNull Long commentId) {
        boolean result = circleService.deleteComment(commentId);
        return YunkeResult.success(result);
    }

    /**
     * 我的动态
     */
    @GetMapping("/myCircle")
    @Operation(summary = "我的动态", description = "分页获取当前用户的动态列表")
    @BusinessLog(value = "获取我的动态")
    public YunkeResult<Page<YunkeCircleVo>> getMyCircleList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size) {
        Page<YunkeCircleVo> result = circleService.getMyCircleList(current, size);
        return YunkeResult.success(result);
    }

    /**
     * 获取关注用户动态
     */
    @GetMapping("/followCircle")
    @Operation(summary = "获取关注用户动态", description = "分页获取关注用户的动态列表")
    @BusinessLog(value = "获取关注用户动态")
    public YunkeResult<Page<YunkeCircleVo>> getFollowCircleList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size) {
        Page<YunkeCircleVo> result = circleService.getFollowCircleList(current, size);
        return YunkeResult.success(result);
    }

    /**
     * 举报动态
     */
    @PostMapping("/report/{circleId}")
    @Operation(summary = "举报动态", description = "举报违规动态")
    @BusinessLog(value = "举报动态", params = "circleId,reason")
    public YunkeResult<Boolean> reportCircle(
            @Parameter(description = "动态ID") @PathVariable @NotNull Long circleId,
            @Parameter(description = "举报原因") @RequestParam @NotBlank String reason) {
        boolean result = circleService.reportCircle(circleId, reason);
        return YunkeResult.success(result);
    }

    /**
     * 屏蔽动态
     */
    @PostMapping("/block/{circleId}")
    @Operation(summary = "屏蔽动态", description = "屏蔽不想看到的动态")
    @BusinessLog(value = "屏蔽动态", params = "circleId")
    public YunkeResult<Boolean> blockCircle(
            @Parameter(description = "动态ID") @PathVariable @NotNull Long circleId) {
        boolean result = circleService.blockCircle(circleId);
        return YunkeResult.success(result);
    }
} 